var Promise = require('bluebird');

// var knex = require('knex')({
//     client: 'mysql',
//     connection: {
//         host: '118.24.4.67',
//         user: 'root',
//         password: 'Xiaobei407@',
//         database: 'stream'
//     }
// });

module.exports = {

    getList: function (userid) {
        return new Promise(function (resolve, reject) {
            knex('history')
                .select('*')
                .where({ userid: userid })
                .orderBy('createdate','desc')
                .limit(50)
                .then(function (result) {
                    resolve(result);
                }).catch(function (e) {
                    reject(e);
                });
        });
    },
    insert: function (data) {
        return new Promise(function (resolve, reject) {            
            knex('history')
                .insert({
                    userid: data.userid,
                    liveid: data.liveid,
                    title: data.title,
                    rtmp: data.rtmp,
                    streamid: data.streamid,
                    hls: data.hls,
                    replaymp4: data.replaymp4,
                    replaym3u8: data.replaym3u8
                })
                .then(function (result) {
                    resolve(result);
                }).catch(function (e) {
                    reject(e);
                });
        });
    }
    /* login: function (username, password) {
         return new Promise(function (resolve, reject) {
             knex('admin')
                 .select('*')
                 .where({ username: username, password: password, status: 1 })
                 .then(function (result) {
                     resolve(result);
                 }).catch(function (e) {
                     reject(e);
                 });
         });
     },
     addUserSession: function (params) {
         return new Promise(function (resolve, reject) {
             knex.transaction(function (trx) {
                 return Promise.all([
                     knex('usersession').where({ userid: params.userid, clientid: params.clientid }).del().transacting(trx),
                     knex('usersession').insert(params).transacting(trx)
                 ])
                     .then(trx.commit)
                     .catch(trx.rollback);
             }).then(function (result) {
                 resolve(result);
             }).catch(function (error) {
                 reject(error);
             });
 
         });
     }
     */
};
